package com.czq.controllor;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czq.common.Result;
import com.czq.pojo.CateGory;
import com.czq.pojo.Dish;
import com.czq.pojo.DishDto;
import com.czq.pojo.DishFlavor;
import com.czq.service.CategoryService;
import com.czq.service.DishService;
import com.czq.service.Dishflavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishControllor {
@Autowired
private DishService  dishService;
@Autowired
private CategoryService categoryService;
@Autowired
private Dishflavor dishflavor;
@Autowired
private RedisTemplate redisTemplate;


    @PostMapping

    public Result<String> adddish(@RequestBody DishDto dishDto){
        dishService.savedish(dishDto);
        Long categoryId = dishDto.getCategoryId();
        String key="dish"+categoryId;
        redisTemplate.delete(key);
        return Result.success("添加菜品成功");

    }
//    菜品分页
    @GetMapping("/page")
    public Result<Page> selectall(Integer page,Integer pageSize,String name){
Page<Dish>    p = new Page(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<Dish>();
        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.orderByAsc(Dish::getCreateTime);
        dishService.page(p,queryWrapper);
Page<DishDto> pp=new Page(page,pageSize);
        BeanUtils.copyProperties(p,pp,"records");
        List<Dish> records = p.getRecords();
        List<DishDto> DishDtoRecords = new ArrayList<DishDto>();

        for (Dish d : records){
            DishDto dto=new DishDto();
            BeanUtils.copyProperties(d,dto);
            Long categoryId = d.getCategoryId();
            CateGory cateGory = categoryService.getById(categoryId);
            String cateGoryName = cateGory.getName();
            dto.setCategoryName(cateGoryName);
            DishDtoRecords.add(dto);
        }
        pp.setRecords(DishDtoRecords);
return Result.success(pp);
    }
//    修改菜品的回显
    @GetMapping("/{id}")
    public  Result<DishDto>  selectbyid(@PathVariable Long id){
        DishDto dishDto = dishService.selectbiyid(id);
     return Result.success(dishDto);
    }

    //回显后修改菜品
    @PutMapping

    public Result<String>  update(@RequestBody DishDto dishDto){

        dishService.updateById(dishDto);
        Long dishid = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> query = new LambdaQueryWrapper();
        query.eq(DishFlavor::getDishId,dishid);
        dishflavor.remove(query);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors){
            flavor.setDishId(dishid);
        }
        dishflavor.saveBatch(flavors);
        Long categoryId = dishDto.getCategoryId();
        String key="dish"+categoryId;
        redisTemplate.delete(key);
        return Result.success("修改成功");
    }

    //查询菜品
    @GetMapping("/list")

    public Result selectdish(@RequestParam("categoryId") Long categoryId){
        List<DishDto> dishDtos=null;

        //动态构造key
        String key="dish"+categoryId;

        //从缓存中取，没有再从数据库里面查
        dishDtos = (List<DishDto>) redisTemplate.opsForValue().get(key);

        if (dishDtos !=null){
            return Result.success(dishDtos);
        }
        dishDtos = new ArrayList<>();

        LambdaQueryWrapper<Dish> query = new LambdaQueryWrapper();
        query.eq(Dish::getCategoryId,categoryId);
        query.eq(Dish::getStatus,1);
        List<Dish> dishes = dishService.list(query);


        for (Dish dish : dishes) {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(dish,dto);
            Long dishId = dish.getId();
            LambdaQueryWrapper<DishFlavor> q = new LambdaQueryWrapper<DishFlavor>();
            q.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> list = dishflavor.list(q);
            dto.setFlavors(list);
            dishDtos.add(dto);
        }
        redisTemplate.opsForValue().set(key,dishDtos);

        return Result.success(dishDtos);

    }
//    批量删除
    @DeleteMapping
    @CacheEvict(value ="setmealCache",allEntries = true)
    public Result<String> deleteDish(@RequestParam  List<Long> ids){

        dishService.removeByIds(ids);

        List<String> keys=new ArrayList<>();
        LambdaQueryWrapper<Dish> query=new LambdaQueryWrapper();
        query.in(Dish::getCategoryId,ids);
        List<Dish> list = dishService.list(query);
        for (Dish d: list){
            Long categoryId = d.getCategoryId();
            String key="dish"+categoryId;
            keys.add(key);
        }
        redisTemplate.delete(keys);
        return Result.success("删除成功");

    }
    //批量起售禁售
    @PostMapping("/status/{status}")

    public Result<String> update(@PathVariable Integer status, @RequestParam List<Long> ids){
        LambdaUpdateWrapper<Dish> q=new LambdaUpdateWrapper<Dish>();
        q.set(Dish::getStatus,status).in(Dish::getId,ids);
        dishService.update(q);

        List<String> keys=new ArrayList<>();
        LambdaQueryWrapper<Dish> query=new LambdaQueryWrapper();
        query.in(Dish::getId,ids);


        List<Dish> list = dishService.list(query);
        for (Dish d: list){
            Long categoryId = d.getCategoryId();
            String key="dish"+categoryId;
            keys.add(key);
        }
        redisTemplate.delete(keys);
        return Result.success("修改成功");

    }
}
